package com.oyyh.lawyermanager.controller;

import com.oyyh.lawyermanager.entity.Lawyer;
import com.oyyh.lawyermanager.service.LawyerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 律师控制器
 *
 * @author 欧阳远辉
 * @version 1.0
 */
@Controller
@RequestMapping("/lawyer")
public class LawyerController {
    @Resource
    private LawyerService lawyerService;

    @PostMapping("/add")
    public String addLawyer(Lawyer lawyer) {
        int result = lawyerService.addLawyer(lawyer);
        if (result == 1) {
            return "../static/success";
        }
        return "../static/fail";
    }

    @GetMapping("/selectAll")
    public String queryLawyer(Model model) {
        List<Lawyer> lawyers = lawyerService.selectAllLawyer();
        model.addAttribute("lawyerList", lawyers);
        return "../static/showAllLawyer";
    }

    @GetMapping("/del")
    public String delLawyer(int lawyerId) {
        int res = lawyerService.delLawyer(lawyerId);
        if (res == 1) {
            return "../static/success";
        }
        return "../static/fail";
    }

    @GetMapping("/selectById")
    public String selectById(int lawyerId, Model model) {
        Lawyer lawyer = lawyerService.selectLawyerById(lawyerId);
        model.addAttribute("lawyer", lawyer);
        return "../static/modifyLawyer";
    }

    @PostMapping("/modify")
    public String modify(Lawyer lawyer){
        int res = lawyerService.modifyLawyer(lawyer);
        if (res == 1) {
            return "redirect:/lawyer/selectAll";
        }
        return "../static/fail";
    }
}
